//启用mppt
void buck_Enable(){
  system_buck_enable = 1;
  digitalWrite(ESP_PIN_PWM_EN, HIGH);
  digitalWrite(ESP_PIN_LED, HIGH);
}

//禁用mppt
void buck_Disable(){
  system_buck_enable = 0; 
  digitalWrite(ESP_PIN_PWM_EN, LOW);
  digitalWrite(ESP_PIN_LED, LOW);
  system_pwm_value = 0;
} 

//预测pwm算法
void predictivePWM(){
  if(system_ina_voltage_1 <= 0){
    //当电压输入为零时防止不确定答案
    system_ppwm_value = 0;
  }else if(system_ina_voltage_2 > system_bat_maxv){
    //计算预测 PWM 并存储在变量中
    system_ppwm_value = ((system_pwm_max * system_bat_maxv) / system_ina_voltage_1) * (system_ppwm_margin / 100);
  }              
  else{
    //计算预测 PWM 并存储在变量中
    system_ppwm_value = ((system_pwm_max * system_ina_voltage_2) / system_ina_voltage_1) * (system_ppwm_margin /100);  
  }              
  system_ppwm_value = constrain(system_ppwm_value, 0, system_pwm_max_limit);
}

//pwm调节
void PWM_Modulation(){
  //运行和计算预测pwm
  predictivePWM(); 
  //mppt模式 PWM - 将下限限制为 PPWM，上限限制为最大允许占空比 
  system_pwm_value = constrain(system_pwm_value, system_ppwm_value, system_pwm_max_limit);
  //设置 PWM 占空比并在启用降压时写入 GPIO
  ledcWrite(system_pwm_channel, system_pwm_value);
  //开启 MPPT 降压
  buck_Enable(); 
}

//充电算法
void Charging_Algorithm(){
  if(system_error>0||system_charging_pause==1){
    //当出现错误或充电暂停时关闭 MPPT 降压
    buck_Disable();
  }else{
    if(system_REC==1){                                            
      buck_Disable();                                             //在 PPWM 初始化之前禁用降压
      Serial.println(F("检测到输入电压"));                         //显示串口信息
      Serial.print(F("初始化pwm"));                               //显示串口信息
      Serial.println("");                                        //在串行上显示下一行的换行符  
      MQTT_send_log(F("检测到输入"));
      readSensors();
      predictivePWM();
      system_pwm_value = system_ppwm_value; 
      system_REC = 0;                                             //重置 IUV 恢复布尔标识符
    }else{
      if(system_ina_current_2 > system_bat_current_limit*0.9){system_pwm_value--;}                                                          //电流高于 → 降低占空比
      else if(system_ina_voltage_2 > system_bat_maxv){system_pwm_value--;}                                                              //电压高于 → 降低占空比
      else{                                                                                                                             //MPPT 算法
        if(system_ina_power_1 > system_ina_power_1_prev && system_ina_voltage_1 > system_ina_voltage_1_prev)     {system_pwm_value--;}  //  ↑P ↑V ; →MPP //D--
        else if(system_ina_power_1 > system_ina_power_1_prev && system_ina_voltage_1 < system_ina_voltage_1_prev){system_pwm_value++;}  //  ↑P ↓V ; MPP← //D++
        else if(system_ina_power_1 < system_ina_power_1_prev && system_ina_voltage_1 > system_ina_voltage_1_prev){system_pwm_value++;}  //  ↓P ↑V ; MPP→ //D++
        else if(system_ina_power_1 < system_ina_power_1_prev && system_ina_voltage_1 < system_ina_voltage_1_prev){system_pwm_value--;}  //  ↓P ↓V ; ←MPP  //D--
        else if(system_ina_voltage_2 > system_bat_maxv)                           {system_pwm_value--;}                                 //  MP MV ; 达到 MPP 
        else if(system_ina_voltage_2 < system_bat_maxv)                           {system_pwm_value++;}                                 //  MP MV ; 达到 MPP 
        system_ina_power_1_prev   = system_ina_power_1;                                                                                 //  存储以前记录的功率
        system_ina_voltage_1_prev = system_ina_voltage_1;                                                                               //存储先前记录的电压
      }   
      PWM_Modulation();  
    }
  }
}
